/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.intelligent.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.intelligent.entity.IntelligentController;
import com.thinkgem.jeesite.modules.intelligent.service.IntelligentControllerService;
import com.thinkgem.jeesite.modules.signallight.entity.TSignalLight;
import com.thinkgem.jeesite.modules.signallight.service.TSignalLightService;

/**
 * 智能控制Controller
 * @author xzw
 * @version 2019-04-09
 */
@Controller
@RequestMapping(value = "${adminPath}/intelligent/intelligentController")
public class IntelligentControllerController extends BaseController {

	@Autowired
	private IntelligentControllerService intelligentControllerService;
	@Autowired
	private TSignalLightService tSignalLightService;
	
	@ModelAttribute
	public IntelligentController get(@RequestParam(required=false) String id) {
		IntelligentController entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = intelligentControllerService.get(id);
		}
		if (entity == null){
			entity = new IntelligentController();
		}
		return entity;
	}
	
	@RequiresPermissions("intelligent:intelligentController:view")
	@RequestMapping(value = {"list", ""})
	public String list(IntelligentController intelligentController, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<IntelligentController> page = intelligentControllerService.findPage(new Page<IntelligentController>(request, response), intelligentController); 
		model.addAttribute("page", page);
		return "modules/intelligent/intelligentControllerList";
	}

	@RequiresPermissions("intelligent:intelligentController:view")
	@RequestMapping(value = "form")
	public String form(IntelligentController intelligentController, Model model) {
		model.addAttribute("allSignlights", tSignalLightService.fingAllSignLight());
		model.addAttribute("intelligentController", intelligentController);
		return "modules/intelligent/intelligentControllerForm";
	}

	@RequiresPermissions("intelligent:intelligentController:edit")
	@RequestMapping(value = "save")
	public String save(IntelligentController intelligentController, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, intelligentController)){
			return form(intelligentController, model);
		}
		intelligentControllerService.save(intelligentController);
		addMessage(redirectAttributes, "保存智能控制成功");
		return "redirect:"+Global.getAdminPath()+"/intelligent/intelligentController/?repage";
	}
	
	@RequiresPermissions("intelligent:intelligentController:edit")
	@RequestMapping(value = "delete")
	public String delete(IntelligentController intelligentController, RedirectAttributes redirectAttributes) {
		intelligentControllerService.delete(intelligentController);
		addMessage(redirectAttributes, "删除智能控制成功");
		return "redirect:"+Global.getAdminPath()+"/intelligent/intelligentController/?repage";
	}

}